<?php

namespace app\api\service;

use app\admin\model\AttentionUser;
use DateTime;
use EasyWeChat\Factory;
use EasyWeChat\Kernel\Exceptions\HttpException;
use think\Exception;

/**
 * 部门管理-服务类
 * Class DeptService
 * @package app\admin\service
 */
class UserService extends BaseService
{
    /**
     * 构造函数
     *
     * DeptService constructor.
     */
    protected $model;
    public function __construct()
    {
        $this->model = new AttentionUser();
        parent::__construct();
    }

    public function register()
    {
        try {

            $params = request()->param();
            $name = getter($params, 'name', '');
            if (empty($name)) throw new Exception('请填写孩子姓名');
            $birth = getter($params, 'birth', '');
            if (empty($birth)) throw new Exception('请填写孩子出生年月');
            $gender = getter($params, 'gender', 1);
            if (empty($gender)) throw new Exception('请填写孩子性别');
            $phone = getter($params, 'phone', '');
            if (empty($phone)) throw new Exception('请填写您的联系方式');

            // 出生日期
            $birthDate = new DateTime($birth);
            // 当前日期
            $currentDate = new DateTime();
            $age = $currentDate->diff($birthDate)->y;


            $data = [
                'name' => $name,
                'gender' => $gender,
                'birth' => $birth,
                'age' => $age,
                'phone' => $phone,
            ];
            $userId = $this->model->edit($data);

            return $this->success(['user_id' => $userId]);
        } catch (Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}